package com.water.note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.water.note.entity.Achievement;

import com.water.note.mapper.AchievementMapper;

import com.water.note.service.AchievementService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成果记录Service实现类
 */
@Service
@RequiredArgsConstructor
public class AchievementServiceImpl extends ServiceImpl<AchievementMapper, Achievement> implements AchievementService {



    @Override
    public Page<Achievement> page(Page<Achievement> page, Long userId, Long categoryId, String keyword) {
        LambdaQueryWrapper<Achievement> queryWrapper = new LambdaQueryWrapper<>();
        // 根据用户ID查询
        queryWrapper.eq(Achievement::getUserId, userId);
        // 根据分类ID查询
        if (categoryId != null) {
            queryWrapper.eq(Achievement::getCategoryId, categoryId);
        }
        // 根据关键字查询
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like(Achievement::getTitle, keyword)
                    .or()
                    .like(Achievement::getDescription, keyword)
                    .or()
                    .like(Achievement::getTags, keyword);
        }
        // 按创建时间降序排序
        queryWrapper.orderByDesc(Achievement::getCreateTime);
        return page(page, queryWrapper);
    }

    @Override
    public Achievement getDetailById(Long id) {
        // 参数校验
        if (id == null) {
            return null;
        }
        // 查询成果记录
        Achievement achievement = getById(id);
        if (achievement == null) {
            return null;
        }

        return achievement;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWithImages(Achievement achievement, List<String> imageUrls) {
        // 参数校验
        if (achievement == null) {
            return false;
        }
        // 保存成果记录
        boolean result = save(achievement);
        if (!result) {
            return false;
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWithImages(Achievement achievement, List<String> imageUrls) {
        // 参数校验
        if (achievement == null || achievement.getId() == null) {
            return false;
        }
        // 更新成果记录
        boolean result = updateById(achievement);
        if (!result) {
            return false;
        }

        return true;
    }

    @Override
    public Map<String, Object> getOverviewData(Long userId) {
        Map<String, Object> result = new HashMap<>();
        
        // 查询成果记录总数
        LambdaQueryWrapper<Achievement> countQuery = new LambdaQueryWrapper<>();
        countQuery.eq(Achievement::getUserId, userId);
        long total = count(countQuery);
        result.put("count", total);
        
        // 查询本月新增的成果记录数量
        LocalDateTime firstDayOfMonth = LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth());
        LambdaQueryWrapper<Achievement> newCountQuery = new LambdaQueryWrapper<>();
        newCountQuery.eq(Achievement::getUserId, userId)
                   .ge(Achievement::getCreateTime, firstDayOfMonth);
        long newCount = count(newCountQuery);
        result.put("newCount", newCount);
        
        return result;
    }
} 